#include "dingdanimportwidget.h"
#include "ui_dingdanimportwidget.h"

DingdanImportWidget::DingdanImportWidget(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DingdanImportWidget)
{
    ui->setupUi(this);
    setWindowTitle("单据导入");

    model = new DingdanPickerStandardItemModel();
    ui->tableView->setModel(model);
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);

    connect(ui->tableView, &QTableView::doubleClicked,
            [=](const QModelIndex index)
    {
        Q_UNUSED(index);
        dingdanExchangeWidget.showWithDingdan(model->getDingdan(index.row()));
    });

    connect(&dingdanService, &DingdanService::getImportDingdanSuccess,
            [=](QList<Dingdan*> dingdans)
    {
        model->reset();
        model->insertDingdan(dingdans, 1);
        this->exec();
    });

    connect(&dingdanExchangeWidget, &DingdanExchangeWidget::importDingdanSuccess,
            [=](Dingdan *dingdan)
    {
        this->close();
        emit this->importDingdanSuccess(dingdan);
    });
}

DingdanImportWidget::~DingdanImportWidget()
{
    delete ui;
}

void DingdanImportWidget::setWidgetType(Dingdan::DingdanType widgetType){
    model->setWidgetType(widgetType);
    switch (widgetType) {
    case Dingdan::SaleOut:
        ui->labelTypeName->setText("导入销售单");
        break;
    case Dingdan::SaleBack:
        ui->labelTypeName->setText("导入销退单");
        break;
    case Dingdan::PurchaseIn:
        ui->labelTypeName->setText("导入采购单");
        break;
    default:
        break;
    }
}

void DingdanImportWidget::setDingdansState(Dingdan::DingdanState state){
    this->state = state;
}

void DingdanImportWidget::showAndInit(){
    dingdanService.getImportDingdans(this, state);
}
